//package com.mark.util.wxmp;
//
////import com.mark.taobaoke.Config;
////import com.mark.taobaoke.Const;
////import com.mark.taobaoke.util.JsonUtil;
//
//import com.mark.util.JsonUtil;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.ws.rs.client.Client;
//import javax.ws.rs.client.ClientBuilder;
//import javax.ws.rs.core.Response;
//import java.util.Map;
//
///**
// * Created by macmatilda on 19/07/2017.
// */
//public class WeixinAppClient {
//    private static Logger logger = LoggerFactory.getLogger(WeixinAppClient.class);
//
//    private static Client client = ClientBuilder.newClient();
//
//    public static final String token = "hellotoken";
//    public static final String KEY_ACCESS_TOKEN = "access_token";
//    public static final String KEY_TICKET = "ticket";
//    public static final String KEY_ERROR_CODE = "errcode";
//    public static final String KEY_ERROR_MESSAGE = "errmsg";
//    public static final String KEY_OPENID = "openid";
//    public static final String KEY_UNIONID = "unionid";
//    public static final String KEY_APPID = "appId";
//    public static final String KEY_TIMESTAMP = "timestamp";
//    public static final String KEY_SIGN_TIMESTAMP = "timeStamp";
//    public static final String KEY_NONCESTR = "nonceStr";
//    public static final String KEY_SIGNATURE = "signature";
//
//    public static final String KEY_ERROR_SCOPE = "scope";
//    public static final int RESULT_SUCCESS_INT = 0;
//
//    public static Client getClient() {
//        return client;
//    }
//
//    public static Map getSessionKey(String appId, String appSecret, String code) {
//        try {
//            Response response = getClient().target("https://api.weixin.qq.com/sns/jscode2session")
//                .queryParam("appid", appId)
//                .queryParam("secret", appSecret)
//                .queryParam("js_code", code)
//                .queryParam("grant_type", "authorization_code")
//                .request()
//                .get(Response.class);
//            String resultString = response.readEntity(String.class);
//            Map<String, Object> result = JsonUtil.json2Bean(resultString, Map.class);
//            logger.info(JsonUtil.bean2Json(result));
//            int errorCode = !result.containsKey(KEY_ERROR_CODE) ? RESULT_SUCCESS_INT : (int) result.get(KEY_ERROR_CODE);
//            if (RESULT_SUCCESS_INT == errorCode) {
//                return result;
//            } else {
//                String errorMessage = result.get(KEY_ERROR_MESSAGE).toString();
//                logger.error("getSessionKey errorCode:" + errorCode + ", errorMessage:" + errorMessage);
//                return null;
//            }
//        } catch (Exception e) {
//            logger.error("", e);
//            return null;
//        }
//    }
//
//    public static String getAccessToken(String appId, String appSecret) {
//        try {
//            Response response = getClient().target("https://api.weixin.qq.com/cgi-bin/token")
//                .queryParam("grant_type", "client_credential")
//                .queryParam("appid", appId)
//                .queryParam("secret", appSecret)
//                .request()
//                .get(Response.class);
//            Map result = response.readEntity(Map.class);
//            logger.info(JsonUtil.bean2Json(result));
//            boolean isSuccess = !result.containsKey(KEY_ERROR_CODE);
//            int errorCode = !result.containsKey(KEY_ERROR_CODE) ? RESULT_SUCCESS_INT : (int) result.get(KEY_ERROR_CODE);
//            if (isSuccess) {
//                String accessToken = result.get(KEY_ACCESS_TOKEN).toString();
//                return accessToken;
//            } else {
//                String errorMessage = result.get(KEY_ERROR_MESSAGE).toString();
//                logger.error("get_accesstoken_error errorCode:" + errorCode + ", errorMessage:" + errorMessage);
//                return null;
//            }
//        } catch (Exception e) {
//            logger.error("", e);
//            return null;
//        }
//    }
//}